package com.shuai.AI.ai_app.rag.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.generation.augmentation.ContextualQueryAugmenter;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class LocalRetrievalAugmentationAdvisorConfig {

  @Value("${spring.ai.dashscope.api-key}")
  private String dashScopeApiKey;

  @Bean
  public Advisor localRetrievalAugmentationAdvisor(VectorStore loveAppSimpleVectorStore) {
    return RetrievalAugmentationAdvisor.builder()
        // 配置查询增强器
        .queryAugmenter(
            ContextualQueryAugmenter.builder()
                .allowEmptyContext(true) // 允许空上下文查询
                .build())
        // 配置文档检索器
        .documentRetriever(
            VectorStoreDocumentRetriever.builder()
                .vectorStore(loveAppSimpleVectorStore)
                //                    .similarityThreshold(0.5) // 相似度阈值
                //                    .topK(3) // 返回文档数量
                //                    .filterExpression(
                //                        new FilterExpressionBuilder().eq("genre",
                // "fairytale").build()) // 文档过滤表达式
                .build())
        .build();
  }
}
